<template>
	<view>
		<view class="goods-list">
			<view v-for="(item, i) in goodsList" :key="i" @click="gotoDetail(item)">
				<my-goods :goods="item"></my-goods>
			</view>
		</view>
	</view>
</template>

<script>
	export default {
		data() {
			return {
				//请求参数对象
				queryObj:{
					query:'',
					cid:'',
					pageNum:1,
					pageSize:10
				},
				goodsList:[],
				total:0,
				//节流阀
				isLoading:false
			};
		},
		onLoad(options){			
			this.queryObj.query = options.query || '';
			this.queryObj.cid = options.cid || '';
			this.getGoodsList();
		},
		methods:{
			async  getGoodsList(cb){
				//打开节流阀
				this.isLoading = true;
				// 3.1 发起请求
				const { data: res } = await uni.$http.post('/api/shopData.php',
				{
					getType:6,
					cid:this.queryObj.cid,
					query:this.queryObj.query,
					pageNum:this.queryObj.pageNum,
					pageSize:this.queryObj.pageSize
				});
				//关闭节流阀
				this.isLoading = false;
				cb && cb();
				//加载失败
				if(res.meta.status !== 200) return uni.$showMsg();
				
				this.total = res.data.total;
				this.goodsList = [...this.goodsList, ...res.data.goods]; 
			},
			gotoDetail(item){
				uni.navigateTo({
					url:'/subpkg/goods_detail/goods_detail?goods_id='+item.goods_id
				})
				
			}
		},
		onReachBottom(){
			if(this.queryObj.pageNum * this.queryObj.pageSize >= this.total) return uni.$showMsg('数据加载完成!');
			if(this.isLoading) return;
			this.queryObj.pageNum++;
			this.getGoodsList()
		},
		onPullDownRefresh(){
			//重置参数
			this.queryObj.pageNum = 1;
			this.total = 0;
			this.isLoading = false;
			this.goodsList = [];
			//请求参数
			this.getGoodsList(()=>uni.stopPullDownRefresh())
		}
	}
</script>

<style lang="scss">

</style>
